﻿Imports System.Data.OleDb
Imports DTO


Public Class ChiTietHoaDonDAO

    Public Function ThemChiTietHoaDon(ByVal cthd As ChiTietHoaDonDTO) As Boolean
        Dim kq As Boolean = True
        Dim cn As OleDbConnection
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        cn = DatabaseProvider.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim sql As String
        sql = "insert into ChiTietHoaDon(idHoaDon,SoLuong,DonGiaBan,idSach,DaXoa) values(? ,? ,? ,? ,?) "
        Dim cmd As New OleDbCommand(sql, cn)

        cmd.Parameters.Add("@idHoaDon", OleDbType.Integer)
        cmd.Parameters.Add("@SoLuong", OleDbType.Integer)
        cmd.Parameters.Add("@DonGiaBan", OleDbType.Integer)
        cmd.Parameters.Add("@idSach", OleDbType.Integer)
        cmd.Parameters.Add("@DaXoa", OleDbType.Integer)
        cmd.Parameters("@idHoaDon").Value = cthd.LayIDHoaDon
        cmd.Parameters("@SoLuong").Value = cthd.LaySoLuong
        cmd.Parameters("@DonGiaBan").Value = cthd.LayDonGiaBan
        cmd.Parameters("@idSach").Value = cthd.LayIDSach
        cmd.Parameters("@DaXoa").Value = 0
        cmd.ExecuteNonQuery()

        cn.Close()
        Return kq
    End Function

    Public Function LayChiTietHoaDon(ByVal cthd As ChiTietHoaDonDTO) As DataTable
        Dim kq As New DataTable


        Dim cn As OleDbConnection
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        cn = DatabaseProvider.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim sql As String = "select idHoaDon,SoLuong,DonGiaban, idSach  from ChiTietHoaDon where idHoaDon = ? "  ' ' ' ''
        Dim cmd As New OleDbCommand(sql, cn)
        cmd.Parameters.Add("@idHoaDon", OleDbType.Integer)
        cmd.Parameters("@idHoaDon").Value = cthd.LayIDHoaDon
        Dim a As New OleDb.OleDbDataAdapter(sql, cn)
        a.SelectCommand = cmd
        a.Fill(kq)
        cn.Close()
        Return kq
    End Function


    Public Function RefreshChiTietHoaDon() As DataTable
        Dim kq As New DataTable


        Dim cn As OleDbConnection
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        cn = DatabaseProvider.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim sql As String = "select idHoaDon,SoLuong,DonGiaban, idSach  from ChiTietHoaDon where  DaXoa= 0"  ' ' ' ''
        Dim cmd As New OleDbCommand(sql, cn)
        Dim a As New OleDb.OleDbDataAdapter(sql, cn)
        a.Fill(kq)
        cn.Close()
        Return kq
    End Function

    Public Sub UpdateChiTietHoaDon(ByVal id As HoaDonDTO)
        Dim cn As OleDbConnection
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        cn = DatabaseProvider.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim sql As String
        sql = "update ChitietHoaDon set DaXoa=1 where idHoaDon= ? "
        Dim cmd As New OleDbCommand(sql, cn)
        cmd.Parameters.Add("@idHoaDon", OleDbType.WChar)
        cmd.Parameters("@idHoaDon").Value = id.LayIDHoaDon
        cmd.ExecuteNonQuery()
        cn.Close()
    End Sub
    ' ' ' 'dùng để xóa thể loại sách
    Public Sub DeleteSach_Loai(ByVal idSach_Loai As TheLoaiDTO)

        Dim kq As Boolean = True
        Dim cn As OleDbConnection
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        cn = DatabaseProvider.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim sql As String
        sql = "Delete from  Sach , ChiTietHoaDon  where Sach.idSach = ChiTietHoaDon.idSach and Sach.idTheLoai= '" & idSach_Loai.LayIdTheLoai & "'  "
        Dim cmd As New OleDbCommand(sql, cn)
        'cmd.Parameters.Add("@Sach.idTheLoaih", OleDbType.Integer)ho
        'cmd.Parameters("@Sach.idTheLoai").Value = idSach_Loai.LayIdTheLoai
        cmd.ExecuteNonQuery()

        cn.Close()
    End Sub

End Class
